package com.xoms.admin.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xoms.admin.controller.res.bean.RProStatusBean;
import com.xoms.admin.controller.res.bean.RProStatusLastTimeBean;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.model.ProStatusBean;
import com.xoms.admin.persist.dao.ProStatusDao;
import com.xoms.admin.service.ProStatusService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.AppTextUtil;
import com.xoms.util.DateTimeUtil;
import com.xoms.util.NumUtil;
import com.xoms.util.StringUtil;

@Service
public class ProStatusServiceImpl implements ProStatusService {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	ProStatusDao proStatusImpl;

	@Override
	public PageResult<RProStatusBean> getStatuslist(AccountBean account, String productId, int page, int pageSize) {
		PageResult<RProStatusBean> result = new PageResult<RProStatusBean>();
		List<RProStatusBean> resList = new ArrayList<RProStatusBean>();
		// 获取价格数量
		int count = proStatusImpl.selectCount(productId);
		if (count > 0) {
			// 数量不为0时获取价格列表
			List<ProStatusBean> lists = proStatusImpl.selectList(productId, page, pageSize);

			if (lists != null) {
				RProStatusBean rp = null;
				for (ProStatusBean price : lists) {
					rp = new RProStatusBean();
					rp = parse(price);
					resList.add(rp);
				}
			}
		}
		result.setData(resList);
		result.setCount(count);
		return result;
	}
	
	/**
	 * 格式化数据
	 * @param status
	 * @return
	 */
	private RProStatusBean parse(ProStatusBean status) {
		RProStatusBean res = new RProStatusBean();
		res.setAccount(status.getAccount());
		res.setProductId(status.getProductId());
		res.setRemark(status.getRemark());
		res.setSid(status.getSid());
		res.setStatus(status.getStatus());
		res.setCreateTime(DateTimeUtil.formatTimestamp(status.getCreateTime()));
		res.setStartTime(DateTimeUtil.formatTimestamp(status.getStartTime()));
		res.setEndTime(DateTimeUtil.formatTimestamp(status.getEndTime()));
		return res;
	}

	@Override
	public RProStatusBean edit(AccountBean account, String sid, String productId, String status, String remark,
			String startTime, String endTime) {
		boolean flag = false;
		ProStatusBean data = new ProStatusBean();
		data.setProductId(productId);
		data.setStatus(NumUtil.toInt(status, 1));// 默认为上架
		data.setRemark(remark);
		data.setStartTime(DateTimeUtil.toTimestamp(startTime, "yyyy-MM-dd HH:mm:ss"));
		data.setEndTime(DateTimeUtil.toTimestamp(endTime, "yyyy-MM-dd HH:mm:ss"));
		if (StringUtil.isEmpty(sid)) {// 新建
			// 新建时判断开始时间是不是在最后生效时间之后
			Timestamp lastEndTime = proStatusImpl.selectLastEndTime(productId);
			if(lastEndTime != null) {
				long lasteTime = DateTimeUtil.formatTimestamp(lastEndTime);
				if(DateTimeUtil.formatTimestamp(data.getStartTime()) < lasteTime) {
					throw new ServiceException("pro_status_startTime_error");
				}
			}
			sid = AppTextUtil.getPrimaryKey();// id
			data.setSid(sid);
			data.setAccountId(account.getAccid());
			flag = proStatusImpl.insert(data) > 0;
			if (!flag) {
				throw new ServiceException("database_insert_fail","商品状态");
			}
		} else {
			data.setSid(sid);
			flag = proStatusImpl.update(data) > 0;// 更新数据库
			if (!flag) {
				throw new ServiceException("database_update_fail","商品状态");
			}
		}
		return parse(proStatusImpl.selectOneById(sid));
	}

	@Override
	public boolean del(AccountBean account, String sid) {
		ProStatusBean temp = proStatusImpl.selectOneBySidInNow(sid);
		if(temp != null) {
			throw new ServiceException("product_status_is_on");
		}
		if (proStatusImpl.del(sid) > 0) {// 删除状态
			return true;
		}
		return false;
	}

	@Override
	public boolean onSale(AccountBean account, String sid) {
		if (proStatusImpl.updateStatus(sid, 1) > 0) {// 更新状态
			return true;
		}

		return false;
	}

	@Override
	public boolean soldOut(AccountBean account, String sid) {
		if (proStatusImpl.updateStatus(sid, 0) > 0) {// 更新状态
			return true;
		}

		return false;
	}

	@Override
	public RProStatusLastTimeBean getLastEndTime(AccountBean account, String productId) {
		RProStatusLastTimeBean result = new RProStatusLastTimeBean();
		Timestamp lastEndTime = proStatusImpl.selectLastEndTime(productId);
		if(lastEndTime == null) {
			return null;
		}
		long lasteTime = DateTimeUtil.formatTimestamp(lastEndTime);
		result.setEndTime(lasteTime);
		return result;
	}
	
}
